<!doctype html>
<html lang="en" data-bs-theme="auto">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>网站信息</title>
  <link href="/assets/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/assets/icons/font/bootstrap-icons.css">
  <link href="/css/dashboard.css" rel="stylesheet">
  <script src="/js/jquery.min.js"></script>
</head>
<body>

  <div class="container-fluid">
    <h5 class="card-title mt-2">网站LOGO</h5>
    <hr>
    <div class="row">
      <div class="col-12">
        <form id="form" action="/admin/sitelogopost" method="post">
          <div class="row mb-3">
            <div class="ml-2">（格式为png、svg或jpg）</div>
          </div>
          <div class="row mb-3">
            <div class="col-sm-10 offset-sm-2 ml-2"><div id="progress-bar-result">
              <% std::string templogourl=obj["info"]["sitelogo"].to_string();  if(templogourl.size()>0){ %>
              <a href="<% echo << obj["info"]["sitelogo"].to_string(); %>?<% echo << timeid(); %>" target="_blank"><img src="<% echo << obj["info"]["sitelogo"].to_string(); %>?<% echo << timeid(); %>"></a>
              <% } %>
             </div></div>
          </div>
          <div class="row mb-3">
            <label for="sitelogo" class="col-sm-2 col-form-label text-sm-end">选择图片</label>
            <div class="col-sm-6">
              <div class="input-group mb-3">
              <input type="text" class="form-control" id="sitelogo" name="sitelogo" value="<% echo << obj["info"]["sitelogo"].to_string(); %>" placeholder="网站logo">
              <input class="form-control" type="file" style="width:100px" id="relate-sitelogo" accept=".png,.jpg,.jpeg">
              </div>
            </div>
            <div class="col-sm-4">
              <div class="progress" id="progress-bar-show" style="display:none" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
                  <div class="progress-bar" id="progress-bar" style="width: 0%">0</div>
                </div>
              
            </div>
          </div>

          <div class="row mb-3">
            <div class="ml-2 col-sm-10 offset-sm-2">
            <div class="form-check">
              <input class="form-check-input" type="checkbox" value="1" id="uplogotoimages" name="uplogotoimages">
              <label class="form-check-label" for="uplogotoimages">
                LOGO更新到 /images/logo.jpg 、 /images/logo.svg 或 /images/logo.png 位置
              </label>
            </div>   
          </div>         
          </div>

          <div class="row mb-3">
            <div class="col-sm-10 offset-sm-2">
              <button type="submit" class="btn btn-primary">确认</button>
              <input type="hidden" id="sid" name="sid" value="<% echo<<obj["info"]["sid"].to_string(); %>" />
            </div>
          </div>

        </form>
      </div>
    </div>

  </div>
  
  <div class="modal fade" id="myfiletobigModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h1 class="modal-title fs-5" id="staticBackdropLabel">提示</h1>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
           文件太大了，请传4M以下的文件！
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
           
        </div>
      </div>
    </div>
  </div>

  <div class="modal fade" id="myfilesuccessModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="myfilesuccess" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h1 class="modal-title fs-5" id="myfilesuccess">提示</h1>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
           上传成功！
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">确定</button>
           
        </div>
      </div>
    </div>
  </div>
  

  <script src="/assets/dist/js/bootstrap.bundle.min.js"></script>
  <script lang="javascript">

$(document).ready(function(e) {
  
        $("#relate-sitelogo").change(function(){
            var fd=new FormData();
            if($(this).get(0).files[0].size>4596640){
                $('#myfiletobigModal').modal('show');
                return false;
            }
            fd.append("upfile",$(this).get(0).files[0]);
            $("#progress-bar-show").show();
            $("#progress-bar-result").hide();
            $.ajax({
                url:"/imageapi/upload?action=uploadimage",
                type:"POST",
                processData: false,
                contentType:false,
                data: fd,
                dataType: "json",
                xhr: function() {  // custom xhr
                    myXhr = $.ajaxSettings.xhr();
                    if(myXhr.upload){ // check if upload property exists
                        myXhr.upload.addEventListener('progress',function(evt){
                            var percentComplete = Math.round(evt.loaded*100 / evt.total);
                            $( "#progress-bar").css("width",percentComplete*2);
                            $( "#progress-bar").html(percentComplete+'%');
                        }, false); // for handling the progress of the upload
                    }
                    return myXhr;
                },
                success:function(data){
                    if(data.state=="SUCCESS"){
                        $("#progress-bar-show").hide();
                        $("#progress-bar-result").show();
                        $("#sitelogo").val(data.url);
						$("#progress-bar-result").html("<a href='"+data.url+"' target=_blank><img src=\""+data.url+"\"/></a>");
                       $('#myfilesuccessModal').modal('show');
                    }
                     
                }
            });
        });
    });	  
  </script>
</body>

</html>